from django.shortcuts import render
# Create your views here.
from .models import *
from .ser import *
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.paginator import Paginator
from django.contrib.auth.hashers import make_password,check_password
from utils.comm import send_message,get_redis_conn
import redis

class NewsAPI(APIView):
    # 新闻添加
    def post(self,request):
        ser = NewsSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code":10010})
    # 新闻展示
    def get(self,request):
        cpage = request.GET.get('cpage')
        news = News.objects.all()
        f = Paginator(news,2)
        paggall = f.page(cpage)
        ser = NewsSer(paggall,many=True)
        return Response({"code":200,"list":ser.data,"numm":f.num_pages})
class AdvertisAPI(APIView):
    # 广告添加
    def post(self,request):
        ser = AdvertisAPISer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code":10010})
    # 广告展示
    def get(self,request):
        adv = Advertis.objects.all()
        ser = AdvertisAPISer(adv,many=True)
        return Response({"code":200,"list":ser.data})




#发短息
class Text(APIView):
    def get(self,request):
        print(send_message('18331568255','5201314',1))
        return Response({"code":200})

class Taa(APIView):
    def get(self,request):
        conn = get_redis_conn()
        conn.setex('name', 10, 'cik')
        names = conn.get('name')
        return Response(names)
import re
#注册
class RegAPI(APIView):

    def post(self,request):
    #注册账号
        #是否为空
        username = request.data.get('username')
        if not username:
            return Response({"code":10010})
        #正则
        reg = re.match('\w{5,12}$',username)
        if not reg:
            return Response({"code":10011})
        #是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({"code":10012})
    #注册手机号
        mobile = request.data.get('mobile')
        if not mobile:
            return Response({"code": 10013})
        # 正则
        regmo = re.match('^1[3-9]\d{9}$', mobile)
        if not regmo:
            return Response({"code": 10014})
        # 是否唯一
        user = User.objects.filter(mobile=mobile).first()
        if user:
            return Response({"code": 10015})
    #注册密码
        password = request.data.get('password')
        if not password:
            return Response({"code":10016})
        regpwd = re.match('^\w{6,12}$',password)
        if not regpwd:
            return Response({"code": 10017})
        # 信息验证
        xinxi = request.data.get('xinxi')
        conn = get_redis_conn()
        code = conn.get(mobile)
        if code:
            code = str(code,encoding="utf8")
        if xinxi != code:
            return Response({"code":10018})
        return Response({"code":200})


#发信息
import random
class sedSms(APIView):
    def post(self,request):
        mobile = request.data.get('mobile')
        code = random.randint(100000,999999)
        res = send_message(mobile,str(code),1)
        codes = 10010
        if res:
            codes=200
            conn = get_redis_conn()
            conn.setex(mobile,60,code)
        return Response({"code":codes})


















































































